package com.ruoyi.work.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.CoordinateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.work.mapper.TbLocationUserMapper;
import com.ruoyi.work.domain.TbLocationUser;
import com.ruoyi.work.service.ITbLocationUserService;

/**
 * 人员定位信息Service业务层处理
 *
 * @author szm
 * @date 2025-10-21
 */
@Slf4j
@Service
public class TbLocationUserServiceImpl extends ServiceImpl<TbLocationUserMapper,TbLocationUser> implements ITbLocationUserService {

    @Autowired
    private RedisCache redisCache;

    @Override
    public boolean insertLocationUser(TbLocationUser tbLocationUser) {
        System.out.println("APP定位信息:"+ JSON.toJSONString(tbLocationUser));
        double[] locatin = CoordinateUtils.wgs84ToGcj02(Double.parseDouble(tbLocationUser.getLongitude()),Double.parseDouble(tbLocationUser.getLatitude()));
        System.out.println("获取的数据："+locatin);
        if (locatin != null){
            tbLocationUser.setLongitude(String.valueOf(locatin[0]));
            tbLocationUser.setLatitude(String.valueOf(locatin[1]));

            if (redisCache.hasKey(redisCache.getCacheObject(tbLocationUser.getUserNo()))){
                TbLocationUser locationUser = redisCache.getCacheObject(tbLocationUser.getUserNo());
                if (locationUser.getLongitude().equals(tbLocationUser.getLongitude()) &&
                        locationUser.getLatitude().equals(tbLocationUser.getLatitude())){
                    log.info("定位数据已存在！！！");
                }else{
                    redisCache.setCacheObject(tbLocationUser.getUserNo(),tbLocationUser);
                    return this.saveOrUpdate(tbLocationUser);
                }
            }else{
                redisCache.setCacheObject(tbLocationUser.getUserNo(),tbLocationUser);
                return this.saveOrUpdate(tbLocationUser);
            }

        }
        return true;
    }
}
